package com.gitee.qdbp.jdbc.support.convert;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.ConverterRegistry;

/**
 * 日期转换类
 *
 * @author zhaohuihua
 * @version 20211031
 */
public class DateConverter {

    /** LocalDateTime转换为Date **/
    public static Date convertToDate(LocalDateTime source) {
        ZoneId zoneId = ZoneId.systemDefault();
        // Combines this date-time with a time-zone to create a ZonedDateTime.
        ZonedDateTime zdt = source.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /** LocalDate转换为Date **/
    public static Date convertToDate(LocalDate source) {
        ZoneId zoneId = ZoneId.systemDefault();
        // Combines this date-time with a time-zone to create a ZonedDateTime.
        ZonedDateTime zdt = source.atStartOfDay().atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /** LocalTime转换为Date **/
    public static Date convertToDate(LocalTime source) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), source);
        ZoneId zoneId = ZoneId.systemDefault();
        // Combines this date-time with a time-zone to create a ZonedDateTime.
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /** Date转换为LocalDateTime **/
    public static LocalDateTime convertToLocalDateTime(Date source) {
        Instant instant = source.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /** Date转换为LocalDate **/
    public static LocalDate convertToLocalDate(Date source) {
        Instant instant = source.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /** Date转换为LocalTime **/
    public static LocalTime convertToLocalTime(Date source) {
        Instant instant = source.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalTime();
    }

    /** 注册日期转换处理类到ConverterRegistry **/
    public static void registerConverters(ConversionService conversionService) {
        if (conversionService instanceof ConverterRegistry) {
            ConverterRegistry registry = (ConverterRegistry) conversionService;
            if (!conversionService.canConvert(Date.class, LocalDateTime.class)) {
                registry.addConverter(new DateToLocalDateTimeConverter());
            }
            if (!conversionService.canConvert(Date.class, LocalDate.class)) {
                registry.addConverter(new DateToLocalDateConverter());
            }
            if (!conversionService.canConvert(Date.class, LocalTime.class)) {
                registry.addConverter(new DateToLocalTimeConverter());
            }
            if (!conversionService.canConvert(LocalDateTime.class, Date.class)) {
                registry.addConverter(new LocalDateTimeToDateConverter());
            }
            if (!conversionService.canConvert(LocalDate.class, Date.class)) {
                registry.addConverter(new LocalDateToDateConverter());
            }
            if (!conversionService.canConvert(LocalTime.class, Date.class)) {
                registry.addConverter(new LocalTimeToDateConverter());
            }
        }
    }
}
